﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ObjectSweptVolume.CApObjectSweptVolumeSimulationHandler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.SweptVolume;
using System;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ObjectSweptVolume;

public class CApObjectSweptVolumeSimulationHandler
{
  private bool m_isInitialized;
  private bool m_isSimulationStarted;

  public CApObjectSweptVolumeSimulationHandler()
  {
    this.m_isSimulationStarted = TxApplication.ActiveDocument.SimulationPlayer.CurrentTime > 0.0 || TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning();
  }

  public void Initialize()
  {
    if (this.m_isInitialized)
      return;
    this.RegisterForSimulationEvents();
    this.m_isInitialized = true;
  }

  public void UnInitialize()
  {
    if (!this.m_isInitialized)
      return;
    this.UnRegisterForSimulationEvents();
    this.m_isInitialized = false;
  }

  public bool MaySample() => this.IsSimulationStarted && !this.IsSilentSimulation;

  public bool IsSimulationStarted => this.m_isSimulationStarted;

  public bool IsSimulationRunning
  {
    get => TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning();
  }

  public bool IsSilentSimulation => !TxApplication.ActiveDocument.SimulationPlayer.RefreshMode;

  public double CurrentSimulationTime
  {
    get
    {
      double currentSimulationTime = 0.0;
      try
      {
        currentSimulationTime = TxApplication.ActiveDocument.SimulationPlayer.CurrentTime;
      }
      catch
      {
      }
      return currentSimulationTime;
    }
  }

  public event samplingOccurenceReachedEventHandler samplingOccurenceReached;

  private void TriggerSamplingOccurenceReachedEvent()
  {
    if (!this.MaySample() || this.samplingOccurenceReached == null)
      return;
    this.samplingOccurenceReached((object) this, new EventArgs());
  }

  public event CApObjectSweptVolumeSimulationHandler.SimulationEventHandler simulationEventOccurred;

  protected virtual void triggerSimulationEventOccurred(
    CApObjectSweptVolumeSimulationHandler.eSimulationEventType eventType)
  {
    if (this.simulationEventOccurred == null)
      return;
    this.simulationEventOccurred((object) this, new CApObjectSweptVolumeSimulationHandler.SimulationEventArgs(eventType));
  }

  private void RegisterForSimulationEvents()
  {
    TxSimulationPlayer simulationPlayer = TxApplication.ActiveDocument.SimulationPlayer;
    simulationPlayer.SimulationStartedForward += new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.SimPlayer_SimulationStartedForward);
    simulationPlayer.SimulationEndedForward += new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.SimPlayer_SimulationEndedForward);
    simulationPlayer.SimulationStopped += new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimPlayer_SimulationStopped);
    simulationPlayer.SimulationCleared += new TxSimulationPlayer_SimulationClearedEventHandler(this.SimPlayer_SimulationCleared);
    simulationPlayer.SegmentSimulationCleared += new TxSimulationPlayer_SegmentSimulationClearedEventHandler(this.SimPlayer_SegmentSimulationCleared);
    simulationPlayer.TimeIntervalReached += new TxSimulationPlayer_TimeIntervalReachedEventHandler(this.SimPlayer_TimeIntervalReached);
  }

  private void UnRegisterForSimulationEvents()
  {
    TxSimulationPlayer simulationPlayer = TxApplication.ActiveDocument.SimulationPlayer;
    simulationPlayer.SimulationStartedForward -= new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.SimPlayer_SimulationStartedForward);
    simulationPlayer.SimulationEndedForward -= new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.SimPlayer_SimulationEndedForward);
    simulationPlayer.SimulationStopped -= new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimPlayer_SimulationStopped);
    simulationPlayer.SimulationCleared -= new TxSimulationPlayer_SimulationClearedEventHandler(this.SimPlayer_SimulationCleared);
    simulationPlayer.SegmentSimulationCleared -= new TxSimulationPlayer_SegmentSimulationClearedEventHandler(this.SimPlayer_SegmentSimulationCleared);
    simulationPlayer.TimeIntervalReached -= new TxSimulationPlayer_TimeIntervalReachedEventHandler(this.SimPlayer_TimeIntervalReached);
  }

  private void SimPlayer_TimeIntervalReached(
    object sender,
    TxSimulationPlayer_TimeIntervalReachedEventArgs args)
  {
    if (args.CurrentTime <= 0.0)
      return;
    this.TriggerSamplingOccurenceReachedEvent();
  }

  private void SimPlayer_SegmentSimulationCleared(
    object sender,
    TxSimulationPlayer_SegmentSimulationClearedEventArgs args)
  {
    this.triggerSimulationEventOccurred(CApObjectSweptVolumeSimulationHandler.eSimulationEventType.Cleared);
  }

  private void SimPlayer_SimulationCleared(
    object sender,
    TxSimulationPlayer_SimulationClearedEventArgs args)
  {
    this.triggerSimulationEventOccurred(CApObjectSweptVolumeSimulationHandler.eSimulationEventType.Cleared);
  }

  private void SimPlayer_SimulationStopped(
    object sender,
    TxSimulationPlayer_SimulationStoppedEventArgs args)
  {
    this.triggerSimulationEventOccurred(CApObjectSweptVolumeSimulationHandler.eSimulationEventType.Stopped);
  }

  private void SimPlayer_SimulationEndedForward(
    object sender,
    TxSimulationPlayer_SimulationEndedForwardEventArgs args)
  {
    this.triggerSimulationEventOccurred(CApObjectSweptVolumeSimulationHandler.eSimulationEventType.Stopped);
  }

  private void SimPlayer_SimulationStartedForward(
    object sender,
    TxSimulationPlayer_SimulationStartedForwardEventArgs args)
  {
    this.m_isSimulationStarted = true;
    this.triggerSimulationEventOccurred(CApObjectSweptVolumeSimulationHandler.eSimulationEventType.Started);
    this.TriggerSamplingOccurenceReachedEvent();
  }

  public enum eSimulationEventType
  {
    None,
    Started,
    Stopped,
    Cleared,
  }

  public class SimulationEventArgs
  {
    public readonly CApObjectSweptVolumeSimulationHandler.eSimulationEventType EventType;

    public SimulationEventArgs(
      CApObjectSweptVolumeSimulationHandler.eSimulationEventType eventType)
    {
      this.EventType = eventType;
    }
  }

  public delegate void SimulationEventHandler(
    object sender,
    CApObjectSweptVolumeSimulationHandler.SimulationEventArgs args);
}
